package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class CoinInfoServiceImpl implements CoinInfoService{
    protected static final Logger LOG = LoggerFactory.getLogger(CoinInfoServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private CoinInfoMapper coinInfoMapper;

    @Override
    public CoinInfo create(CoinInfo item) {
         coinInfoMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return coinInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(CoinInfo item) {
        return coinInfoMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public CoinInfo get(Integer id) {
        return coinInfoMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<CoinInfo> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,CoinInfo item){
       CoinInfoExample  example = new CoinInfoExample();
       CoinInfoExample.Criteria criteria = example.createCriteria();
       if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCoinEnglishName() != null ){
            criteria.andCoinEnglishNameEqualTo(item.getCoinEnglishName());
        }
        if(item.getCoinSymbol() != null ){
            criteria.andCoinSymbolEqualTo(item.getCoinSymbol());
        }
        if(item.getCoinUrl() != null ){
            criteria.andCoinUrlEqualTo(item.getCoinUrl());
        }
        if(item.getCoinChineseName() != null ){
            criteria.andCoinChineseNameEqualTo(item.getCoinChineseName());
        }
        if(item.getCoinContractAddress() != null ){
            criteria.andCoinContractAddressEqualTo(item.getCoinContractAddress());
        }
        if(item.getCoinDecimal() != null ){
            criteria.andCoinDecimalEqualTo(item.getCoinDecimal());
        }
        if(item.getCoinDecimalUsed() != null ){
            criteria.andCoinDecimalUsedEqualTo(item.getCoinDecimalUsed());
        }
        if(item.getCoinQuotesName() != null ){
            criteria.andCoinQuotesNameEqualTo(item.getCoinQuotesName());
        }
        if(item.getCoinLocation() != null ){
            criteria.andCoinLocationEqualTo(item.getCoinLocation());
        }
        if(item.getCoinPriceRmb() != null ){
            criteria.andCoinPriceRmbEqualTo(item.getCoinPriceRmb());
        }
        if(item.getCoinPriceUsdt() != null ){
            criteria.andCoinPriceUsdtEqualTo(item.getCoinPriceUsdt());
        }
        if(item.getCoinFluctuation() != null ){
            criteria.andCoinFluctuationEqualTo(item.getCoinFluctuation());
        }
        if(item.getCoinPriceUpdatetime() != null ){
            criteria.andCoinPriceUpdatetimeEqualTo(item.getCoinPriceUpdatetime());
        }
        if(item.getCoinFluctuationUpdatetime() != null ){
            criteria.andCoinFluctuationUpdatetimeEqualTo(item.getCoinFluctuationUpdatetime());
        }
        if(item.getCoinPriceRequestpath() != null ){
            criteria.andCoinPriceRequestpathEqualTo(item.getCoinPriceRequestpath());
        }
        if(item.getCoinPriceJsonStructure() != null ){
            criteria.andCoinPriceJsonStructureEqualTo(item.getCoinPriceJsonStructure());
        }
        if(item.getCoinPriceRequestmethod() != null ){
            criteria.andCoinPriceRequestmethodEqualTo(item.getCoinPriceRequestmethod());
        }
        if(item.getCoinPriceRequestbody() != null ){
            criteria.andCoinPriceRequestbodyEqualTo(item.getCoinPriceRequestbody());
        }
        if(item.getCoinFluctuationJsonStructure() != null ){
            criteria.andCoinFluctuationJsonStructureEqualTo(item.getCoinFluctuationJsonStructure());
        }
        if(item.getCoinFluctuationRequestpath() != null ){
            criteria.andCoinFluctuationRequestpathEqualTo(item.getCoinFluctuationRequestpath());
        }
        if(item.getCoinFluctuationRequestmethod() != null ){
            criteria.andCoinFluctuationRequestmethodEqualTo(item.getCoinFluctuationRequestmethod());
        }
        if(item.getIsdisplay() != null ){
            criteria.andIsdisplayEqualTo(item.getIsdisplay());
        }
        if(item.getCoinFluctuationRequestbody() != null ){
            criteria.andCoinFluctuationRequestbodyEqualTo(item.getCoinFluctuationRequestbody());
        }
        if(item.getCoinSort() != null ){
            criteria.andCoinSortEqualTo(item.getCoinSort());
        }
        if(item.getIsSync() != null ){
            criteria.andIsSyncEqualTo(item.getIsSync());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<CoinInfo> list = this.coinInfoMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<CoinInfo> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,CoinInfo item){
       CoinInfoExample  example = new CoinInfoExample();
       CoinInfoExample.Criteria criteria = example.createCriteria();
       if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCoinEnglishName() != null ){
            criteria.andCoinEnglishNameEqualTo(item.getCoinEnglishName());
        }
        if(item.getCoinSymbol() != null ){
            criteria.andCoinSymbolEqualTo(item.getCoinSymbol());
        }
        if(item.getCoinUrl() != null ){
            criteria.andCoinUrlEqualTo(item.getCoinUrl());
        }
        if(item.getCoinChineseName() != null ){
            criteria.andCoinChineseNameEqualTo(item.getCoinChineseName());
        }
        if(item.getCoinContractAddress() != null ){
            criteria.andCoinContractAddressEqualTo(item.getCoinContractAddress());
        }
        if(item.getCoinDecimal() != null ){
            criteria.andCoinDecimalEqualTo(item.getCoinDecimal());
        }
        if(item.getCoinDecimalUsed() != null ){
            criteria.andCoinDecimalUsedEqualTo(item.getCoinDecimalUsed());
        }
        if(item.getCoinQuotesName() != null ){
            criteria.andCoinQuotesNameEqualTo(item.getCoinQuotesName());
        }
        if(item.getCoinLocation() != null ){
            criteria.andCoinLocationEqualTo(item.getCoinLocation());
        }
        if(item.getCoinPriceRmb() != null ){
            criteria.andCoinPriceRmbEqualTo(item.getCoinPriceRmb());
        }
        if(item.getCoinPriceUsdt() != null ){
            criteria.andCoinPriceUsdtEqualTo(item.getCoinPriceUsdt());
        }
        if(item.getCoinFluctuation() != null ){
            criteria.andCoinFluctuationEqualTo(item.getCoinFluctuation());
        }
        if(item.getCoinPriceUpdatetime() != null ){
            criteria.andCoinPriceUpdatetimeEqualTo(item.getCoinPriceUpdatetime());
        }
        if(item.getCoinFluctuationUpdatetime() != null ){
            criteria.andCoinFluctuationUpdatetimeEqualTo(item.getCoinFluctuationUpdatetime());
        }
        if(item.getCoinPriceRequestpath() != null ){
            criteria.andCoinPriceRequestpathEqualTo(item.getCoinPriceRequestpath());
        }
        if(item.getCoinPriceJsonStructure() != null ){
            criteria.andCoinPriceJsonStructureEqualTo(item.getCoinPriceJsonStructure());
        }
        if(item.getCoinPriceRequestmethod() != null ){
            criteria.andCoinPriceRequestmethodEqualTo(item.getCoinPriceRequestmethod());
        }
        if(item.getCoinPriceRequestbody() != null ){
            criteria.andCoinPriceRequestbodyEqualTo(item.getCoinPriceRequestbody());
        }
        if(item.getCoinFluctuationJsonStructure() != null ){
            criteria.andCoinFluctuationJsonStructureEqualTo(item.getCoinFluctuationJsonStructure());
        }
        if(item.getCoinFluctuationRequestpath() != null ){
            criteria.andCoinFluctuationRequestpathEqualTo(item.getCoinFluctuationRequestpath());
        }
        if(item.getCoinFluctuationRequestmethod() != null ){
            criteria.andCoinFluctuationRequestmethodEqualTo(item.getCoinFluctuationRequestmethod());
        }
        if(item.getIsdisplay() != null ){
            criteria.andIsdisplayEqualTo(item.getIsdisplay());
        }
        if(item.getCoinFluctuationRequestbody() != null ){
            criteria.andCoinFluctuationRequestbodyEqualTo(item.getCoinFluctuationRequestbody());
        }
        if(item.getCoinSort() != null ){
            criteria.andCoinSortEqualTo(item.getCoinSort());
        }
        if(item.getIsSync() != null ){
            criteria.andIsSyncEqualTo(item.getIsSync());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<CoinInfo> list = this.coinInfoMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,CoinInfo item){
        CoinInfoExample  example = new CoinInfoExample();
        CoinInfoExample.Criteria criteria = example.createCriteria();
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCoinEnglishName() != null ){
            criteria.andCoinEnglishNameEqualTo(item.getCoinEnglishName());
        }
        if(item.getCoinSymbol() != null ){
            criteria.andCoinSymbolEqualTo(item.getCoinSymbol());
        }
        if(item.getCoinUrl() != null ){
            criteria.andCoinUrlEqualTo(item.getCoinUrl());
        }
        if(item.getCoinChineseName() != null ){
            criteria.andCoinChineseNameEqualTo(item.getCoinChineseName());
        }
        if(item.getCoinContractAddress() != null ){
            criteria.andCoinContractAddressEqualTo(item.getCoinContractAddress());
        }
        if(item.getCoinDecimal() != null ){
            criteria.andCoinDecimalEqualTo(item.getCoinDecimal());
        }
        if(item.getCoinDecimalUsed() != null ){
            criteria.andCoinDecimalUsedEqualTo(item.getCoinDecimalUsed());
        }
        if(item.getCoinQuotesName() != null ){
            criteria.andCoinQuotesNameEqualTo(item.getCoinQuotesName());
        }
        if(item.getCoinLocation() != null ){
            criteria.andCoinLocationEqualTo(item.getCoinLocation());
        }
        if(item.getCoinPriceRmb() != null ){
            criteria.andCoinPriceRmbEqualTo(item.getCoinPriceRmb());
        }
        if(item.getCoinPriceUsdt() != null ){
            criteria.andCoinPriceUsdtEqualTo(item.getCoinPriceUsdt());
        }
        if(item.getCoinFluctuation() != null ){
            criteria.andCoinFluctuationEqualTo(item.getCoinFluctuation());
        }
        if(item.getCoinPriceUpdatetime() != null ){
            criteria.andCoinPriceUpdatetimeEqualTo(item.getCoinPriceUpdatetime());
        }
        if(item.getCoinFluctuationUpdatetime() != null ){
            criteria.andCoinFluctuationUpdatetimeEqualTo(item.getCoinFluctuationUpdatetime());
        }
        if(item.getCoinPriceRequestpath() != null ){
            criteria.andCoinPriceRequestpathEqualTo(item.getCoinPriceRequestpath());
        }
        if(item.getCoinPriceJsonStructure() != null ){
            criteria.andCoinPriceJsonStructureEqualTo(item.getCoinPriceJsonStructure());
        }
        if(item.getCoinPriceRequestmethod() != null ){
            criteria.andCoinPriceRequestmethodEqualTo(item.getCoinPriceRequestmethod());
        }
        if(item.getCoinPriceRequestbody() != null ){
            criteria.andCoinPriceRequestbodyEqualTo(item.getCoinPriceRequestbody());
        }
        if(item.getCoinFluctuationJsonStructure() != null ){
            criteria.andCoinFluctuationJsonStructureEqualTo(item.getCoinFluctuationJsonStructure());
        }
        if(item.getCoinFluctuationRequestpath() != null ){
            criteria.andCoinFluctuationRequestpathEqualTo(item.getCoinFluctuationRequestpath());
        }
        if(item.getCoinFluctuationRequestmethod() != null ){
            criteria.andCoinFluctuationRequestmethodEqualTo(item.getCoinFluctuationRequestmethod());
        }
        if(item.getIsdisplay() != null ){
            criteria.andIsdisplayEqualTo(item.getIsdisplay());
        }
        if(item.getCoinFluctuationRequestbody() != null ){
            criteria.andCoinFluctuationRequestbodyEqualTo(item.getCoinFluctuationRequestbody());
        }
        if(item.getCoinSort() != null ){
            criteria.andCoinSortEqualTo(item.getCoinSort());
        }
        if(item.getIsSync() != null ){
            criteria.andIsSyncEqualTo(item.getIsSync());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<CoinInfo> list = this.coinInfoMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(coinInfo -> JSONObject.parseObject(JSONObject.toJSONString(coinInfo))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

    @Override
    public CoinInfo selectCoinInfoBycoinSymbol(String coinSymbol) {

        CoinInfoExample  example = new CoinInfoExample();
        CoinInfoExample.Criteria criteria = example.createCriteria();
        criteria.andCoinSymbolEqualTo(coinSymbol);
        List< CoinInfo> list = this.coinInfoMapper.selectByExample(example);
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

}
